package cn.biplam.back.services.ajxresponse;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import cn.biplam.common.interfaces.AjxExchangeServer;
import cn.biplam.common.tools.Call;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * 页面请求交换接口实现类
 * @author WangSong 2009-9-23下午04:18:37
 * @version 1.0
 */
public class AjxExchangeServerImpl extends UnicastRemoteObject implements AjxExchangeServer{
	
	private static final long serialVersionUID = 2238245953653790846L;
	private Lock mLock=new ReentrantLock();
	private int iCount=0;
	private static KvMap<String, String[]>map;
	
	/**
	 * 构造方法
	 * @throws RemoteException
	 */
	public AjxExchangeServerImpl() throws RemoteException {
		iCount++;
		map=new KvMap<String, String[]>();
	}
	
	/**
	 * 取请求页面
	 * @param action 行为id
	 * @param params 参数容器
	 * @param sHomeUrl 系统路径
	 * @param mUser 用户信息
	 * @return 请求的页面
	 * @throws RemoteException
	 * @throws ErrorMessage
	 */
	public String get(String action, 
					  KvMap<String, String> params,
			          String homeUrl, 
			          UserInfo user) throws RemoteException, ErrorMessage {
		String content = null;
		addCount();
		try{
			content = getContent(action, params, homeUrl, user);
		}catch(ErrorMessage e){
			throw e;
		}catch(Exception e){
			throw new ErrorMessage(e,"AjxExchangeServerImpl.get failed("+action+") ");
		}finally{
			reduceCount();
		}
		return content;
	}
	
	/**
	 * 取请求页面
	 * @param action 行为id
	 * @param params 参数容器
	 * @param sHomeUrl 系统路径
	 * @param mUser 用户信息
	 * @return 请求的页面
	 * @throws RemoteException
	 * @throws ErrorMessage
	 */
	private String getContent(String action, 
			  				  KvMap<String, String> params,
			  				  String homeUrl, 
			  				  UserInfo user) throws ErrorMessage{
		/**根据action查询配置表中操作类*/
		String[] classPath = getClassByActionId(action);
		if(classPath==null || classPath[0]==null){
			throw new ErrorMessage("AjxExchangeServerImpl error,action is not found:"+action);
		}else if(!"1".equals(classPath[1])){
			/**判断模型是否发布*/
			throw new ErrorMessage("AjxExchangeServerImpl error,action is not authorized:"+action);
		}
		
		Call call = null;
		try{
			/**创建操作类的实例*/
			call = new Call(classPath[0]);
		}catch(ErrorMessage e){
			String addMsg="\n\tat AjxExchangeServerImpl,class is:"+classPath[0];
			if(e.getMessage()==null){
				e.setMessage(addMsg);
			}else{
				e.setMessage(e.getMessage()+addMsg);
			}
			throw e;
		}	
		return (String)call.run("get", action,params,homeUrl,user);
	}
	
	/**
	 * 根据行为编码来查询实现类
	 * @param actionId 行为编码id
	 * @return 实现类
	 * @throws ErrorMessage 
	 */
	private String[] getClassByActionId(String actionId) throws ErrorMessage{
		String[] classInfo=map.getByKey(actionId);//查询静态容器
		if(classInfo!=null)
			return classInfo;
		SqlResult sr = new AjxServiceOpt().getById(actionId.substring(0,actionId.length()-2));
		if(TOOLS.exist(sr)){
			classInfo=new String[]{sr.get(0,"s_class_path"),sr.get(0,"publish")};
			map.put(actionId, classInfo);//放入静态容器
			return classInfo;
		}else{
			throw new ErrorMessage("actionId is not found:"+actionId);
		}
	}

	/**服务监控用*/
	public int getCount() throws RemoteException {
		return this.iCount;
	}

	private synchronized void addCount() {
		this.iCount++;
	}

	private synchronized void reduceCount() {
		this.iCount--;
	}

	public void lock() throws RemoteException {
		mLock.lock();
	}

	public boolean trylock() throws RemoteException {
		return mLock.tryLock();
	}

	public void unlock() {
		mLock.unlock();
	}
}
